package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.ContentInfo;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrater on 2015/12/28.
 */
public class TieZiAdapter extends BaseAdapter {
    private Context mContext;
    private List<ContentInfo> mList = new ArrayList<ContentInfo>();

    public TieZiAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<ContentInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.tiezi_item, null);
            childHolder.mTitle = (TextView) convertView.findViewById(R.id.title);
            childHolder.mNeiRong = (TextView) convertView.findViewById(R.id.neirong);
            childHolder.mAuthor = (TextView) convertView.findViewById(R.id.author);
            childHolder.mTime = (TextView) convertView.findViewById(R.id.time);
            childHolder.mPinglun = (TextView) convertView.findViewById(R.id.pinglun);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        ContentInfo cinfo = mList.get(position);
        if (cinfo != null) {
            childHolder.mTitle.setText(cinfo.getTitle());
            childHolder.mNeiRong.setText(cinfo.getContent());

            if (!TextUtils.isEmpty(cinfo.getNickName())) {
                try {
                    childHolder.mAuthor.setText(URLDecoder.decode(cinfo.getNickName(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            childHolder.mTime.setText(cinfo.getCreateDate());
            childHolder.mPinglun.setText(cinfo.getCommentCount());

        }

        return convertView;
    }

    class ChildHolder {
        TextView mTitle;
        TextView mNeiRong;
        TextView mAuthor;
        TextView mTime;
        TextView mPinglun;
    }
}